package com.bjsdzk.collector.one2one;

import com.bjsdzk.collector.entity.agreement.CallBackCmd;
import com.bjsdzk.collector.entity.agreement.SendCmd;
import com.bjsdzk.collector.serviceImp.DictionariesService;
import com.bjsdzk.collector.serviceImp.ExceColleContrService;
import com.bjsdzk.collector.serviceImp.ParamsSettingService;
import com.bjsdzk.collector.utils.CRC16;
import com.bjsdzk.collector.utils.Utils;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.datagram.DatagramSocket;
import io.vertx.core.datagram.DatagramSocketOptions;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.SocketAddress;
import io.vertx.ext.mongo.MongoClient;
import io.vertx.redis.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.zip.CRC32;

/**
 * @author pc
 */
public final class BasetVerticleServer extends BasetVerticle {
    private static Logger logger = LoggerFactory.getLogger(BasetVerticleServer.class);

    public BasetVerticleServer(RedisClient redis, MongoClient mongoClient, ExceColleContrService exceColleContrService, ParamsSettingService paramsSettingService, DictionariesService dictionariesService) {
        this.redis = redis;
        this.mongoClient = mongoClient;
        this.exceColleContrService = exceColleContrService;
        this.paramsSettingService = paramsSettingService;
        this.dictionariesService = dictionariesService;
    }

    @Override
    public void start() {
        getInit();
        NetServer server = getNetServer();
        server.connectHandler(netSocket -> {
            ConcurrentLinkedQueue<CallBackCmd> returnQueue = new ConcurrentLinkedQueue<>();
            PriorityBlockingQueue<SendCmd> sendQueue = new PriorityBlockingQueue<>();
            Map<String, Long> keepLiveMap = new ConcurrentHashMap<>(16);
            defaultStart(netSocket, keepLiveMap, sendQueue, returnQueue);
            ReceiveServer receiveServer = new ReceiveServer();
            netSocket.handler(receiveServer);
        });
        serverListen(server,redis,"mod_"+port);

    }

    @Override
    public void stop() {

    }

    class ReceiveServer implements Handler<Buffer> {

        @Override
        public void handle(Buffer buffer) {
            logger.info("port:{},dataStr:{}",port,buffer.toString());
            logger.info("port:{},dataHex:{}",port,Utils.bytes2String(buffer.getBytes()));
            logger.info("port:{},length:{}",port,buffer.getBytes().length);
        }
    }
}

